package artifacts;

import java.awt.EventQueue;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import java.util.Dictionary;
import java.util.Map;
import java.util.HashMap;

import javax.swing.JOptionPane;
import javax.swing.*;
import java.awt.event.*;
//import cartago.OPERATION;
import cartago.tools.*;
import cartago.*;

public class GuiDomainUser extends GUIArtifact {
    
    public GuiView view = null;
	public Map<String, String> descriptors = new HashMap<String, String>();
	//public bool checking;
    
	public void setup() {
		view = new GuiView();
		
		linkActionEventToOp(view.btnStorage,"ok");
		
		defineObsProperty("prueba",-1);
		defineObsProperty("newDescriptors",-1);
		defineObsProperty("saveDescriptors",-1,"");
		
		view.setVisible(true);		
	}
	/*
    public void init(){
	
		/*view = new GuiView();
		linkActionEventToOp(view.btnStorage,"ok");
        
		defineObsProperty("prueba",-1);
		defineObsProperty("newDescriptors",-1);
    }*/
	
	
	@OPERATION void showDescriptors(String document_id, String descriptors){
		/*view = new GuiView(document_id);
		linkActionEventToOp(view.btnStorage,"ok");*/
		view.setVisible(true);
		view.showDescriptors(descriptors);
		view.setDocumentId(document_id);
		
    }
	
	@INTERNAL_OPERATION void closed(WindowEvent ev){
		signal("closed");
	}
	
	@INTERNAL_OPERATION void ok(ActionEvent ev){
		getObsProperty("saveDescriptors").updateValue(0,"1");
		getObsProperty("saveDescriptors").updateValue(1,view.getSelectedDescriptors());
	}
	
	@OPERATION void prueba(String aux)
	{
		ObsProperty prop = getObsProperty("prueba");
		prop.updateValue(2);
	}
	
	@OPERATION void start(String documentId){
		//checking = true;
		this.execInternalOp("checkingProcess",documentId);
	}

	@OPERATION void stop(){
		//checking = false;
	}

	@INTERNAL_OPERATION void checkingProcess(String documentId)
	{/*
		while (checking){
			chequearCambios(documentId);
			await_time(2000);
		}*/
	}
	
	public void chequearCambiosssss(String documentId)
	{
		if(descriptors.containsKey(documentId))
		{
			ObsProperty prop = getObsProperty("newDescriptors");
			prop.updateValue(documentId);
		}
	}
	
	public void descriptorsReady(String document, String new_descriptors)
	{
		descriptors.put(document,new_descriptors);
		ObsProperty prop = getObsProperty("newDescriptors");
		prop.updateValue(2);
	}
    
    // Actions that simulate the building progress

    
    class GuiView extends JFrame {        
        
		private JPanel contentPane;
		private JTextField textField;
		private JTextField textField_1;
		private JTextField textField_2;
		private JTextField textField_3;
		private JTable table_6;
		private DefaultTableModel modelDescriptors;
		//private GuiDomainUser father;
		private String document_id;
		private JButton btnStorage;
		private String descriptorSelected;
        
        public GuiView(){
			//document_id = documentid;
			//father = objeto;
            setTitle(" -- Gui Domain User -- ");
            setSize(800,600);
			setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			setBounds(100, 100, 660, 411);
			contentPane = new JPanel();
			contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
			setContentPane(contentPane);
			contentPane.setLayout(null);
			
			JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
			tabbedPane.setBounds(0, 73, 644, 300);
			contentPane.add(tabbedPane);
			
			JPanel panel_5 = new JPanel();
			tabbedPane.addTab("Document Storage", null, panel_5, null);
			panel_5.setLayout(null);
			
			JLabel lblDescriptors = new JLabel("Descriptors");
			lblDescriptors.setBounds(36, 26, 119, 14);
			panel_5.add(lblDescriptors);
			
			JScrollPane scrollPane_8 = new JScrollPane();
			scrollPane_8.setBounds(36, 40, 233, 120);
			panel_5.add(scrollPane_8);
			
			//table_6 = new JTable();
			modelDescriptors = new DefaultTableModel(){
			Class[] columnTypes = new Class[] {
				Object.class, Boolean.class
			};
			public Class getColumnClass(int columnIndex) {
				return columnTypes[columnIndex];
			}
			boolean[] columnEditables = new boolean[] {
				false, true
			};
			public boolean isCellEditable(int row, int column) {
				return columnEditables[column];
			}
			};
			modelDescriptors.addColumn("Descriptors");
			modelDescriptors.addColumn("");
			table_6 = new JTable(modelDescriptors);
			//table_6.setModel(modelDescritors);
			table_6.getColumnModel().getColumn(0).setPreferredWidth(130);
			table_6.getColumnModel().getColumn(1).setPreferredWidth(35);
			scrollPane_8.setViewportView(table_6);
			
			btnStorage = new JButton("Storage");
			btnStorage.setBounds(179, 187, 89, 23);
			btnStorage.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					descriptorSelected = getDescriptors();
				}
			});
			panel_5.add(btnStorage);
			JLabel lblTask = new JLabel("Task:");
			lblTask.setBounds(10, 27, 46, 14);
			contentPane.add(lblTask);
			
			JLabel lblWorker = new JLabel("Worker:");
			lblWorker.setBounds(157, 27, 46, 14);
			contentPane.add(lblWorker);
			
			JLabel lblGroup = new JLabel("Group:");
			lblGroup.setBounds(312, 27, 46, 14);
			contentPane.add(lblGroup);
			
			JLabel lblWorkflow = new JLabel("Workflow:");
			lblWorkflow.setBounds(486, 27, 61, 14);
			contentPane.add(lblWorkflow);
			
			textField = new JTextField();
			textField.setBounds(40, 24, 86, 20);
			textField.setEditable(false);
			textField.setName("task");
			contentPane.add(textField);
			textField.setColumns(10);
			
			textField_1 = new JTextField();
			textField_1.setBounds(202, 24, 86, 20);
			textField_1.setEditable(false);
			contentPane.add(textField_1);
			textField_1.setColumns(10);
			
			textField_2 = new JTextField();
			textField_2.setBounds(359, 24, 86, 20);
			textField_2.setEditable(false);
			contentPane.add(textField_2);
			textField_2.setColumns(10);
			
			textField_3 = new JTextField();
			textField_3.setBounds(548, 24, 86, 20);
			textField_3.setEditable(false);
			contentPane.add(textField_3);
			textField_3.setColumns(10);
		}
		
		public String getSelectedDescriptors()
		{
			return descriptorSelected;
		}
		
		private String getDescriptors()
		{
			String aux = "";
			for(int i = 0;i < modelDescriptors.getRowCount();i++)
			{
				if((Boolean)modelDescriptors.getValueAt(i, 1) == Boolean.TRUE)
				{
					if(aux.length() > 0)
						aux = aux + "-" + modelDescriptors.getValueAt(i, 0);
					else
						aux += modelDescriptors.getValueAt(i, 0);
				}
			
			}
			return aux;
		}
		
		public void setDocumentId(String documentid)
		{
			document_id = documentid;
		}
		
		public void showDescriptors(String descriptors)
		{
			if(descriptors.length() > 1)
			{
				String[] arrayDescriptors = descriptors.split("-");
				for(int i = 0;i < arrayDescriptors.length;i++)
				{
					Object[] row = {arrayDescriptors[i],Boolean.FALSE};
					modelDescriptors.addRow(row);
				}
				
			}
			
		}
		
        
    }

}
